package by.dkusch.aircompany.webapp.page.tech.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.filter.FilterPlaneType;
import by.dkusch.aircompany.model.tables.PlaneType;
import by.dkusch.aircompany.model.tables.PlaneType_;
import by.dkusch.aircompany.services.PlaneTypeService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.tech.PlaneTypePage;

public class PlaneTypeListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private PlaneTypeService planeTypeService;
	private EntityDataProviderFilter<Long, PlaneType, FilterPlaneType> planeTypeDataProvider;

	public PlaneTypeListPanel(String id, int itemsPerPage) {
		super(id);

		planeTypeDataProvider = new EntityDataProviderFilter<Long, PlaneType, FilterPlaneType>(planeTypeService);
		planeTypeDataProvider.setSort(PlaneType_.code, SortOrder.ASCENDING);
		planeTypeDataProvider.setFilterState(new FilterPlaneType());

		setOutputMarkupId(true);

		DataView<PlaneType> dataView = new DataView<PlaneType>("planeTypeList", planeTypeDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<PlaneType> item) {
				final PlaneType planeType = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("name"));
				item.add(new Label("passClass1"));
				item.add(new Label("passClass2"));
				item.add(new Label("passClass3"));
				item.add(new Label("crewCount"));
				item.add(new Label("length"));
				item.add(new Label("weight"));
				item.add(new CommonActionPanel<Long, PlaneType, PlaneTypePage>("action-panel", planeTypeService, planeType, PlaneTypePage.class, PlaneTypeListPanel.this));

			}
		};

		FilterForm<FilterPlaneType> filterForm = new FilterForm<FilterPlaneType>("filterForm", planeTypeDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(planeTypeDataProvider, "filterState.code")).setLabel(new ResourceModel("plane.type.list.code")));
		filterForm.add(new TextField<String>("filter-name", PropertyModel.<String> of(planeTypeDataProvider, "filterState.name")).setLabel(new ResourceModel("plane.type.list.name")));
		filterForm.add(new TextField<String>("filter-pc1-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass1Min")).setLabel(new ResourceModel("filter-pc1-min")));
		filterForm.add(new TextField<String>("filter-pc1-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass1Max")).setLabel(new ResourceModel("filter-pc1-max")));
		filterForm.add(new TextField<String>("filter-pc2-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass2Min")).setLabel(new ResourceModel("filter-pc2-min")));
		filterForm.add(new TextField<String>("filter-pc2-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass2Max")).setLabel(new ResourceModel("filter-pc2-max")));
		filterForm.add(new TextField<String>("filter-pc3-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass3Min")).setLabel(new ResourceModel("filter-pc3-min")));
		filterForm.add(new TextField<String>("filter-pc3-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.passClass3Max")).setLabel(new ResourceModel("filter-pc3-max")));
		filterForm.add(new TextField<String>("filter-crew-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.crewCountMin")).setLabel(new ResourceModel("filter-crew-min")));
		filterForm.add(new TextField<String>("filter-crew-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.crewCountMax")).setLabel(new ResourceModel("filter-crew-max")));
		filterForm.add(new TextField<String>("filter-length-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.lengthMin")).setLabel(new ResourceModel("filter-length-min")));
		filterForm.add(new TextField<String>("filter-length-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.lengthMax")).setLabel(new ResourceModel("filter-length-max")));
		filterForm.add(new TextField<String>("filter-weight-min", PropertyModel.<String> of(planeTypeDataProvider, "filterState.weightMin")).setLabel(new ResourceModel("filter-weight-min")));
		filterForm.add(new TextField<String>("filter-weight-max", PropertyModel.<String> of(planeTypeDataProvider, "filterState.weightMax")).setLabel(new ResourceModel("filter-weight-max")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("planetype-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, PlaneType, PlaneTypePage>("new-panel", new PlaneType(), PlaneTypePage.class));

		divTable.add(new OrderByBorder<SingularAttribute<PlaneType, ?>>("sortByName", PlaneType_.name, planeTypeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<PlaneType, ?>>("sortByCode", PlaneType_.code, planeTypeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<PlaneType, ?>>("sortByLength", PlaneType_.length, planeTypeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<PlaneType, ?>>("sortByWeight", PlaneType_.weight, planeTypeDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);

	}

}
